package com.ebring.shareEducation.controller;


import com.ebring.shareEducation.bean.response.ReleaseUser;
import com.ebring.shareEducation.bean.OrderSheet;
import com.ebring.shareEducation.bean.Taskrelease;
import com.ebring.shareEducation.bean.UserReviews;
import com.ebring.shareEducation.service.CommentsService;
import com.ebring.shareEducation.service.CommentsreplyService;
import com.ebring.shareEducation.service.TaskreleaseService;
import com.ebring.shareEducation.service.UserReviewsService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TaskreleaseController {

    private final ResourceLoader resourceLoader;

    @Autowired
    private TaskreleaseService taskreleaseService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private UserReviewsService userReviewsService;

    @Autowired
    private CommentsreplyService commentsreplyService;


    @Autowired
    public TaskreleaseController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }


    @Value("${web.upload-path}")
    private String path;

    /**
     * 查询所有任务列表
     *
     * @return
     */
    @PostMapping("/taskrelease/list")
    public RspMsg<ReleaseUser> select(Integer pageNum,Integer pageSize,String phone,String auditStates) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        return taskreleaseService.selectTaskrelease(phone,auditStates);
    }

    /**
     * 后台审核查询任务详情
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectdetails")
    public RspMsg<Taskrelease> selectDetails(Taskrelease taskrelease) {
        return taskreleaseService.selectDetails(taskrelease);
    }

    /**
     * 手机查看任务详情
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectdetailss")
    public RspMsg<Taskrelease> selectDetailss(Taskrelease taskrelease,String role) {
        Map<String, Object> maps = (Map<String, Object>) taskreleaseService.selectDetailss(taskrelease).getResult();
        ReleaseUser releaseUser = (ReleaseUser) maps.get("taskrelease");

        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        if (releaseUser == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到相关信息");
            return rspMsg;
        }
        String taskid = releaseUser.getTaskReleaseId();
        UserReviews userReviews = new UserReviews();

        userReviews.setTaskReleaseId(taskid);
        userReviews.setRole(role);
        List<UserReviews> list = userReviewsService.selectTaskReviewe(userReviews);
        if(list!=null&&!list.isEmpty()){
            UserReviews userReviews2=list.get(0);
            releaseUser.setComment(userReviews2.getReviewContent());
            releaseUser.setStar(userReviews2.getRating());
            releaseUser.setEffect(userReviews2.getEffect());
            releaseUser.setManner(userReviews2.getManner());
            if(role.equals("01")){
                releaseUser.setRemark1(userReviews2.getRemark1());
                releaseUser.setRemark2(userReviews2.getRemark2());
                releaseUser.setRemark3(userReviews2.getRemark3());
                releaseUser.setRemark4(userReviews2.getRemark4());
                releaseUser.setRemark5(userReviews2.getRemark5());
            }else{
                releaseUser.setRemark6(userReviews2.getRemark6());
                releaseUser.setRemark7(userReviews2.getRemark7());
                releaseUser.setRemark8(userReviews2.getRemark8());
                releaseUser.setRemark9(userReviews2.getRemark9());
                releaseUser.setRemark10(userReviews2.getRemark10());
            }

            releaseUser.setAvg(userReviews2.getAvg());
            map.put("taskreleases", releaseUser);
            rspMsg.setOK();
            rspMsg.setResult(map);
        }else{
            map.put("taskreleases", releaseUser);
            rspMsg.setOK();
            rspMsg.setMsg("暂无评价");
            rspMsg.setResult(map);
        }

        return rspMsg;
    }

    /**
     * 手机查看任务详情
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectuserdetail")
    public RspMsg<Taskrelease> selectuserDetail(Taskrelease taskrelease) {
        Map<String, Object> maps = (Map<String, Object>) taskreleaseService.selectDetailss(taskrelease).getResult();
        ReleaseUser releaseUser = (ReleaseUser) maps.get("taskrelease");

        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        if (releaseUser == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到相关信息");
            return rspMsg;
        }
        String taskid = releaseUser.getTaskReleaseId();
        UserReviews userReviews = new UserReviews();

        userReviews.setTaskReleaseId(taskid);
        List<UserReviews> list = userReviewsService.selectTaskReviewe(userReviews);
        if(list!=null&&!list.isEmpty()){
            UserReviews userReviews2=list.get(0);
            releaseUser.setComment(userReviews2.getReviewContent());
            releaseUser.setStar(userReviews2.getRating());
            map.put("taskreleases", releaseUser);
            rspMsg.setOK();
            rspMsg.setResult(map);
        }else{
            map.put("taskreleases", releaseUser);
            rspMsg.setOK();
            rspMsg.setMsg("暂无评价");
            rspMsg.setResult(map);
        }

        return rspMsg;
    }


    /**
     * 审核发布任务
     */
    @PostMapping("/taskrelease/auditTaskRelease")
    public RspMsg<Taskrelease> auditTaskRelease(String taskReleaseId, String auditStates) {
        return taskreleaseService.auditTaskRelease(taskReleaseId, auditStates);
    }

    /**
     * 修改任务
     */
    @PostMapping("/taskrelease/update")
    public RspMsg<Taskrelease> updateTaskrelease(Taskrelease taskrelease) {
        return taskreleaseService.updateTaskrelease(taskrelease);
    }

    /**
     * 自增查看人数
     */
    @PostMapping("/taskrelease/updatenumber")
    public RspMsg<Taskrelease> updatenumber(Taskrelease taskrelease) {

        return taskreleaseService.updateNumber(taskrelease);
    }

    /**
     * 根据任务ID删除任务
     *
     * @param taskId 菜单id
     * @return
     */

    @GetMapping("/taskrelease/deleteById")
    public RspMsg<Taskrelease> deleteById(String taskId) {
        return taskreleaseService.deleteById(taskId);
    }


    /**
     * 新增任务
     *
     * @param taskrelease
     * @return
     */

    @PostMapping("/taskrelease/insertTaskrelease")
    public RspMsg<Taskrelease> insertTaskrelease(Taskrelease taskrelease) {
        return taskreleaseService.insertTaskrelease(taskrelease);
    }

    /**
     * 查询用户下面的任务
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/select")
    public RspMsg<Taskrelease> selectTaskrelease(Taskrelease taskrelease) {
        return taskreleaseService.selectUTaskrelease(taskrelease);
    }

    /**
     * 根据任务状态查询
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectByStates")
    public RspMsg<Taskrelease> selectByStates(String taskStates) {
        return taskreleaseService.selectByStates(taskStates);
    }

    /**
     * 支付金币
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/payGold")
    public RspMsg<Taskrelease> payGold(String userId, String taskReleaseId, String payPassword) {
        return taskreleaseService.payGold(userId, taskReleaseId, payPassword);
    }

    /**
     * 查询订单详情
     * 根据用户查询订单详情
     *
     * @param
     * @return
     */
    @PostMapping("/orderSheet/orderDetails")
    public RspMsg<OrderSheet> orderDetails(String userId) {
        return taskreleaseService.orderDetails(userId);
    }

    /**
     * 任务抢单
     *
     * @param taskReleaseId 单号
     * @return
     */
    @PostMapping("/taskrelease/Robbing")
    public RspMsg<Taskrelease> Robbing(String taskReleaseId, String userId) {
        return taskreleaseService.Robbing(taskReleaseId, userId);
    }

    /**
     * 手机端任务显示，根据加急条件以及订单创建时间
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectTaskReleasex")
    public RspMsg<Taskrelease> selectTaskRelease(Taskrelease taskrelease) {
        return taskreleaseService.selectTaskRelease();
    }

    /**
     * 手机端首页任务展示
     * @param num
     * @return
     */
    @PostMapping("/taskrelease/selectTaskRelease")
    public RspMsg<Taskrelease> selectTaskReleaseIndex(@RequestParam(value = "num", required = false, defaultValue = "10") int num) {
        return taskreleaseService.selectTaskReleaseIndex(num);
    }

    /**
     * 手机端查询用户发布的所有任务
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectUserTaskRelease")
    public RspMsg<Taskrelease> selectUserTaskRelease(Taskrelease taskrelease){
        return taskreleaseService.selectUserTaskRelease(taskrelease);
    }

    /**
     * 手机端查询用户接受的所有任务
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectUserTaskorder")
    public RspMsg<Taskrelease> selectUserTaskOrder(Taskrelease taskrelease){
        return taskreleaseService.selectUserTaskOrder(taskrelease);
    }

    /**
     * 手机端根据任务单号查看任务详情，任务接单人
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectByTaskIdTaskRelease")
    public RspMsg<Taskrelease> selectByTaskIdTaskRelease(Taskrelease taskrelease){
        return taskreleaseService.selectByTaskIdTaskRelease(taskrelease);
    }

    /**
     * 手机端根据任务单号查看任务详情，及评价
     *
     * @param
     * @return
     */
    @PostMapping("/taskrelease/selectTaskreleases")
    public RspMsg<Taskrelease> selectTaskreleases(Taskrelease taskrelease){
        return taskreleaseService.selectTaskreleases(taskrelease);
    }




    /**
     * 修改任务状态(重新发布、确认接单人员、关闭任务)
     */
    @PostMapping("/taskrelease/updateorders")
    public RspMsg<Taskrelease> updateTaskreleaseorders(Taskrelease taskrelease) {
        return taskreleaseService.updateTaskreleaseorders(taskrelease);
    }

    /**
     * 修改任务状态(重新发布、确认接单人员、关闭任务)
     */
    @PostMapping("/taskrelease/updatecode")
    public RspMsg<Taskrelease> updateTaskreleasecode(Taskrelease taskrelease) {
        return taskreleaseService.updateTaskreleasecode(taskrelease);
    }


    /**
     * 根据任务id查询任务
     * @param taskrelease
     * @return
     */
    @PostMapping("/taskrelease/selectTaskIdTaskRelease")
    public RspMsg<Taskrelease> selectTaskIdTaskRelease(Taskrelease taskrelease){
        return   taskreleaseService.selectTaskIdTaskRelease(taskrelease);
    }
}
